package com.example.news;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.news.databinding.FragmentHomeBinding;


public class HomeFragment extends Fragment {
    private FragmentHomeBinding binding;

    public HomeFragment() {
    }


    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       binding=FragmentHomeBinding.inflate(inflater,container,false);
       return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        binding.viewPage.setAdapter(new HomeboardviewPagerAdapter(getChildFragmentManager()));
        HomeboardviewPagerAdapter adapter=new HomeboardviewPagerAdapter(getChildFragmentManager());
        binding.viewPage.setAdapter(adapter);
        binding.viewPage.setOffscreenPageLimit(adapter.getCount()-1);
        binding.tabs.setupWithViewPager(binding.viewPage);
    }

    public static class HomeboardviewPagerAdapter extends FragmentPagerAdapter{

        public HomeboardviewPagerAdapter(@NonNull FragmentManager fm) {
            super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return HomeboardchildFragment.newInstance(position);
        }

        @Override
        public int getCount() {
            return 5;
        }

        public CharSequence getPageTitle(int position){
            switch (position){
                case 0:
                    return "军事";
                case 1:
                    return "生活";
                case 2:
                    return "医疗";
                case 3:
                    return "财经";
                case 4:
                    return "教育";
            }
            return null;
        }
    }

}